package com.yg.controller;

import com.yg.pojo.TBook;
import com.yg.pojo.TOrder;
import com.yg.pojo.vo.PageVo;
import com.yg.service.TBookService;
import com.yg.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    private TBookService bookService;
    @Autowired
    private TOrderService orderService;

    @GetMapping
    public ModelAndView manage(HttpSession session, ModelAndView modelAndView) {
        String user = (String) session.getAttribute("loginUser");
        if (user == null) {
            modelAndView.setViewName("user/login");
        } else {
            modelAndView.setViewName("manager/manager");
        }
        return modelAndView;
    }

    @GetMapping("/book")
    public ModelAndView book(HttpSession session, ModelAndView modelAndView) {
        String user = (String) session.getAttribute("loginUser");
        if (user == null) {
            modelAndView.setViewName("user/login");
        } else {
            PageVo<TBook> page = bookService.allBookPage(1L, 4L, "/manager/book");
            System.out.println(page.toString());
            modelAndView.setViewName("manager/book_manager");
            modelAndView.addObject("page", page);
        }
        return modelAndView;
    }

    @GetMapping("/book/page/{pageNo}/{pageSize}")
    public ModelAndView indexPage(HttpSession session, @PathVariable("pageNo") Long pageNo, @PathVariable("pageSize") Long pageSize, ModelAndView modelAndView) {
        String user = (String) session.getAttribute("loginUser");
        if (user == null) {
            modelAndView.setViewName("user/login");
        } else {
            PageVo<TBook> page = bookService.allBookPage(pageNo, pageSize, "/manager/book");
            modelAndView.setViewName("manager/book_manager");
            modelAndView.addObject("page", page);
        }
        return modelAndView;
    }

    @GetMapping("/bookEdit/{bookId}/{pageNo}/{pageSize}")
    public ModelAndView bookEdit(HttpSession session, @PathVariable("bookId") String bookId, @PathVariable("pageNo") Long pageNo, @PathVariable("pageSize") Long pageSize, ModelAndView modelAndView) {
        String user = (String) session.getAttribute("loginUser");
        if (user == null) {
            modelAndView.setViewName("user/login");
        } else {
            TBook book = bookService.getById(bookId);
            modelAndView.addObject("pageNo", pageNo);
            modelAndView.addObject("pageSize", pageSize);
            modelAndView.addObject("book", book);
            modelAndView.setViewName("manager/book_edit");
        }
        return modelAndView;
    }

    @GetMapping("/addBook")
    public ModelAndView addBook(ModelAndView modelAndView) {
        modelAndView.setViewName("manager/book_add");
        return modelAndView;
    }

    @GetMapping("/order")
    public ModelAndView order(ModelAndView modelAndView, HttpSession session) {
        String user = (String) session.getAttribute("loginUser");
        if (user == null) {
            modelAndView.setViewName("user/login");
        } else {
            modelAndView.setViewName("manager/order_manager");
            PageVo<TOrder> page = orderService.allOrderPage(1L, 4L, "/manager/order");
            modelAndView.addObject("page", page);
        }
        return modelAndView;
    }

    @GetMapping("/order/page/{pageNo}/{pageSize}")
    public ModelAndView orderPage(@PathVariable("pageNo") Long pageNo, @PathVariable("pageSize") Long pageSize, ModelAndView modelAndView, HttpSession session) {
        String user = (String) session.getAttribute("loginUser");
        if (user == null) {
            modelAndView.setViewName("user/login");
        } else {
            modelAndView.setViewName("manager/order_manager");
            PageVo<TOrder> page = orderService.allOrderPage(pageNo, pageSize, "/manager/order");
            modelAndView.addObject("page", page);
        }
        return modelAndView;
    }

    @GetMapping("/fahuo/{orderId}/{pageNo}/{pageSize}")
    public void fahuo(@PathVariable("orderId") String orderId, @PathVariable("pageNo") Long pageNo, @PathVariable("pageSize") Long pageSize, HttpSession session, HttpServletResponse response) throws IOException {
        String user = (String) session.getAttribute("loginUser");
        if (user == null) {
            response.sendRedirect("/login");
        } else {
            orderService.fahuo(orderId);
            response.sendRedirect("/manager/order/page/" + pageNo + "/" + pageSize);
        }

    }
}
